<?php declare(strict_types=1);

namespace application\units;

class Request
{
    
    private $request;
    private $input;
    private $params;
    
    public function __construct()
    {
        $input         = file_get_contents('php://input');
        $this->input   = json_decode($input, true) ?: [];
        $this->request = $_REQUEST;
        $this->params  = array_merge($this->request, $this->input);
    }
    
    public function params($key = null, $default = null)
    {
        if ($key) {
            return isset($this->params[$key]) ? $this->params[$key] : $default;
        }
        return $this->params;
    }
    
    public function isPost()
    {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }
    
    public function ip()
    {
        return $_SERVER['REMOTE_ADDR'];
    }
    
    public function server($key = '')
    {
        if ($key) {
            return isset($_SERVER[$key]) ? $_SERVER[$key] : null;
        }
        return $_SERVER;
    }
}
